package cn.dancepeak.artcle.controller;

import cn.dancepeak.artcle.service.IParamManagerServiceImpl;
import cn.dancepeak.article.domain.ParamManager;
import cn.dancepeak.article.query.ParamManagerQuery;
import cn.dancepeak.exception.BusinessException;
import cn.dancepeak.utils.R;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/paramManager")
@Slf4j
public class ParamManagerController {
    @Autowired
    private IParamManagerServiceImpl paramManagerService;

    //1.增
    @PostMapping("/add")
    public R<Void> add(@RequestBody ParamManager paramManager){
        try {
            paramManagerService.add(paramManager);
            return R.ok("新增成功");
        } catch (BusinessException e) {
            log.info("新增用户异常：{}",e.getMessage());
            return R.fail(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            log.info("新增系统异常：{}",e.getMessage());
            return R.fail(e.getMessage());
        }
    }
    //2.根据ID改
    @PostMapping("/update")
    public R<Void> update(@RequestBody ParamManager paramManager){
        try {
            paramManagerService.update(paramManager);
            return R.ok("修改成功");
        } catch (BusinessException e) {
            log.info("修改用户异常：{}",e.getMessage());
            return R.fail(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            log.info("修改系统异常：{}",e.getMessage());
            return R.fail(e.getMessage());
        }
    }
    //3.根据ID删
    @DeleteMapping("/{id}")
    public R<Void> delete(@PathVariable("id") Long id){
        try {
            paramManagerService.delete(id);
            return R.ok("删除成功");
        } catch (BusinessException e) {
            log.info("删除用户异常：{}",e.getMessage());
            return R.fail(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            log.info("删除系统异常：{}",e.getMessage());
            return R.fail(e.getMessage());
        }
    }
    //4.批量删
    @PostMapping("/deleteByList")
    public R<Void> deleteByList(@RequestBody List<Long> ids){
        try {
            paramManagerService.deleteByList(ids);
            return R.ok("删除成功");
        } catch (BusinessException e) {
            log.info("删除用户异常：{}",e.getMessage());
            return R.fail(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            log.info("删除系统异常：{}",e.getMessage());
            return R.fail(e.getMessage());
        }
    }
    //5.根据ID查
    @GetMapping("/{id}")
    public R<ParamManager> getById(@PathVariable("id") Long id){
        try {
            ParamManager paramManager = paramManagerService.getById(id);
            return R.ok(paramManager);
        } catch (BusinessException e) {
            log.info("查询用户异常：{}",e.getMessage());
            return R.fail(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            log.info("查询系统异常：{}",e.getMessage());
            return R.fail(e.getMessage());
        }
    }
    //6.分页查询
    @PostMapping("/page")
    public R<PageInfo<ParamManager>> page(@RequestBody ParamManagerQuery paramManagerQuery){
        try {
            PageInfo<ParamManager> page = paramManagerService.page(paramManagerQuery);
            return R.ok(page);
        } catch (BusinessException e) {
            log.info("查询用户异常：{}",e.getMessage());
            return R.fail(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            log.info("查询系统异常：{}",e.getMessage());
            return R.fail(e.getMessage());
        }
    }
    //修改参数类型
    @PostMapping("/updateState")
    public R<Void> updateState(@RequestBody ParamManager paramManager){
        try {
            paramManagerService.updateState(paramManager);
            return R.ok();
        } catch (BusinessException e) {
            log.info("修改状态异常：{}",e.getMessage());
            return R.fail(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            log.info("修改状态系统异常：{}",e.getMessage());
            return R.fail(e.getMessage());
        }
    }
}
